//
//  ActivityViewController.m
//  EasyToTesco
//
//  Created by mc on 16/1/31.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ActivityViewController.h"

#import "HeaderFileForEasyToTesco.h"
#import "SDRefresh.h"
#import "ActivityTableViewCell.h"

//导入商品详情页面的头文件
#import "ActivityDetailViewController.h"


@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong)UITableView *ActivityTableview;
//添加刷新控件
@property (nonatomic, weak) SDRefreshHeaderView *refreshHeader;
@property (nonatomic, weak) SDRefreshFooterView *refreshFooter;
@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    NSLog(@"%lf", self.tabBarController.view.frame.size.height);
    [self addActivityUI];
}

- (void)addActivityUI {
    
    if (self.transint == 1) {
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    }
    
    self.titleLabel.text = @"活动";
    [self addActivityTableview];
    
    //添加刷新控件的方法
    [self setupActivityHeader];
    [self setupActivityFooter];
    
}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addActivityTableview {
    self.ActivityTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.firstView.frame), WEITH, HIGHT-CGRectGetHeight(self.firstView.frame)-self.tabBarController.tabBar.frame.size.height) style:UITableViewStylePlain];
    if (self.transint ==1) {
        self.ActivityTableview.frame = CGRectMake(0, CGRectGetMaxY(self.firstView.frame), WEITH, HIGHT-CGRectGetHeight(self.firstView.frame));
    } else {
    self.ActivityTableview.frame = CGRectMake(0, CGRectGetMaxY(self.firstView.frame), WEITH, HIGHT-CGRectGetHeight(self.firstView.frame)-self.tabBarController.tabBar.frame.size.height);
    }
    
    self.ActivityTableview.dataSource = self;
    self.ActivityTableview.delegate = self;
    self.ActivityTableview.rowHeight = 160;
    [self.view addSubview:self.ActivityTableview];
}


#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 16;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifierActivity = @"ActivityTableViewCell";
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierActivity];
    if (cell == nil) {
        cell =  [[[NSBundle mainBundle]loadNibNamed:@"ActivityTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}


#pragma mark -- UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld--%ld", indexPath.section, indexPath.row);
    
    ActivityDetailViewController *activityDetailVc= [[ActivityDetailViewController alloc] init];
    [self presentViewController:activityDetailVc animated:YES completion:nil];
    
}


- (void)setupActivityHeader
{
    //添加下拉刷新对象
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    
    //     默认是在navigationController环境下，如果不是在此环境下，请设置
    refreshHeader.isEffectedByNavigationController = NO;
    
    //加入到目标tableview上
    [refreshHeader addToScrollView:self.ActivityTableview];
    
    //添加下拉加载内容的方法
    __weak SDRefreshHeaderView *weakRefreshHeader = refreshHeader;
    __weak typeof(self) weakSelf = self;
    refreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
#pragma mark -- 加载数据的方法
            
            [weakSelf.ActivityTableview reloadData];
            [weakRefreshHeader endRefreshing];
        });
    };
    
    // 进入页面自动加载一次数据
    [refreshHeader autoRefreshWhenViewDidAppear];
}

- (void)setupActivityFooter
{
    SDRefreshFooterView *refreshFooter = [SDRefreshFooterView refreshView];
    refreshFooter.isEffectedByNavigationController = NO;
    [refreshFooter addToScrollView:self.ActivityTableview];
    [refreshFooter addTarget:self refreshAction:@selector(footerRefresh)];
    _refreshFooter = refreshFooter;
}


- (void)footerRefresh
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
#pragma mark -- 加载数据的方法
        
        [self.ActivityTableview reloadData];
        [self.refreshFooter endRefreshing];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
